package model

import "github.com/jinzhu/gorm"

type ShieldModel struct {
	gorm.Model
	Keyword   string `gorm:"not null;type :varchar(128)" json:"keyword"`
	Category     string `gorm:"not null;type :varchar(32)" json:"category"`
}

// * 设置表名
func (shield ShieldModel ) TableName() string {
	return "shields"
}

// * 添加shiled到数据库
func (shield ShieldModel) Create() error {
	return DB.Self.Create(&shield).Error
}

// * 获取所有shileds
func (shield ShieldModel) GetAll() (uint64, []ShieldModel, error) {

	var shields []ShieldModel
	var count uint64

	if err := DB.Self.Table(shield.TableName()).Count(&count).Error; err != nil {
		return count, shields, err
	}
	if err := DB.Self.Find(&shields).Error; err != nil {
		return count, shields, err
	}
	return count, shields, nil

}

// * 获取某一条shield
func (shield ShieldModel) Get() (ShieldModel, error) {
	return shield, DB.Self.First(&shield, shield.ID).Error
}

// * 修改屏蔽词
func (shield ShieldModel) Update() error {
	return DB.Self.Save(&shield).Error
}